package com.xt.services;

import com.xt.dao.StudentDao;
import com.xt.entity.Student;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class StudentScoreManage {
    public static void insert(Student stu)
    {
        try {
            Connection conn= StudentDao.getConn();  //数据库连接
            PreparedStatement ps=conn.prepareStatement("insert into score(stuCard,stuName,javaScore,mysqlScore,interScore,info)values(?,?,?,?,?,?)");
            ps.setString(1, stu.getStuCard());
            ps.setString(2, stu.getStuName());
            ps.setDouble(3, stu.getJavaScore());
            ps.setDouble(4, stu.getMysqlScore());
            ps.setDouble(5, stu.getInterScore());
            ps.setString(6, stu.getInfo());
            int f=ps.executeUpdate();
            if(f>0)
            {
                JOptionPane.showMessageDialog(null, "成功插入数据");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "没有成功插入数据");
            }
            ps.close();
            conn.close();

        } catch (Exception e) {
            // TODO: handle exception
            JOptionPane.showMessageDialog(null, "注意需要不能重复");
        }
    }
    public static void update(Student stu)
    {
        try {
            Connection con=StudentDao.getConn();
            PreparedStatement ps=con.prepareStatement("update score set stuCard=?,stuName=?,javaScore=?,mysqlScore=?,interScore=?,info=? where stuCard=?");
            ps.setString(1, stu.getStuCard());
            ps.setString(2, stu.getStuName());
            ps.setDouble(3, stu.getJavaScore());
            ps.setDouble(4, stu.getMysqlScore());
            ps.setDouble(5, stu.getInterScore());
            ps.setString(6, stu.getInfo());
            ps.setString(7, stu.getStuCard());
            int f=ps.executeUpdate();
            System.out.println(f);
            if(f>0)
            {
                JOptionPane.showMessageDialog(null, "成功更新数据");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "没有完美的更新数据");
            }
            ps.close();
            con.close();
        } catch (Exception e) {
            // TODO: handle exception
            JOptionPane.showMessageDialog(null, "没有成功更新数据");
        }
    }
    public static void delete(String stuCard)
    {
        try {
            Connection conn=StudentDao.getConn();
            PreparedStatement ps=conn.prepareStatement("delete from score where stuCard=?");
            ps.setString(1,stuCard);
            int f=ps.executeUpdate();
            if(f>0)
            {
                JOptionPane.showMessageDialog(null, "成功删除数据");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "没有删除数据");
            }
            ps.close();
            conn.close();
        } catch (Exception e) {
            // TODO: handle exception
            JOptionPane.showMessageDialog(null, "连接失败");
        }
    }
    public static Vector query()
    {
        try {
            Vector vector=new Vector();
            Connection conn=StudentDao.getConn();
            PreparedStatement ps=conn.prepareStatement("select * from score ");
            ResultSet rs=ps.executeQuery();
            while(rs.next()&&rs.getRow()>0)
            {
                Vector row=new Vector();
                for(int col=1;col<=rs.getMetaData().getColumnCount();col++)
                {
                    if(col==3||col==4)
                    {
                        row.add(String.valueOf(rs.getDouble(col)));
                    }
                    else if(col==5||col==1)
                    {
                        row.add(String.valueOf(rs.getString(col)));
                    }
                    else
                    {
                        row.add(String.valueOf(rs.getString(col)));
                    }
                }
                vector.add(row);
            }
            return vector;
        } catch (Exception e) {
            // TODO: handle exception
            JOptionPane.showMessageDialog(null, "查询失败");
            e.printStackTrace();
            return null;
        }
    }
}
